﻿using BoriPeter.FitnessJournal8.Models;
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.FitnessJournal8.ViewModels
{
    public class AddNewEntryViewModel : ViewModelBase
    {
        #region OnGoBack()
        protected async override void OnGoBack()
        {
            base.OnGoBack();

            var newEntry = new FoodEntry()
            {
                Date = SelectedDate.Date,
                EntryName = Food.Name,
                Meal = SelectedMeal.Name,
                NumberOfUnits = (Decimal)selectedServingInt.Name + ((Decimal)selectedServingDec.Name/(Decimal)10),
                ServingId = Food.SelectedServing.Id
            };
            Food.NewEntry = newEntry;
            await NutritionSource.SaveAsync(Food);
        }
        #endregion

        #region Meal
        private List<Meal> mealList;
        public List<Meal> MealList
        {
            get
            {
                return this.mealList;
            }

            set
            {
                this.mealList = value;
                NotifyOfPropertyChange("MealList");
            }
        }

        private Meal selectedMeal;
        public Meal SelectedMeal
        {
            get
            {
                return this.selectedMeal;
            }

            set
            {
                this.selectedMeal = value;
                NotifyOfPropertyChange("SelectedMeal");
            }
        }
        #endregion

        #region Date
        private List<DateNamed> dateList;
        public List<DateNamed> DateList
        {
            get
            {
                return this.dateList;
            }

            set
            {
                this.dateList = value;
                NotifyOfPropertyChange("DateList");
            }
        }

        private DateNamed selectedDate;
        public DateNamed SelectedDate
        {
            get
            {
                return this.selectedDate;
            }

            set
            {
                this.selectedDate = value;
                NotifyOfPropertyChange("SelectedDate");
            }
        }
        #endregion

        #region Constructor
        public AddNewEntryViewModel(INavigationService navigationService)
            : base(navigationService)
        {

        }
        #endregion

        #region OnInitialize
        protected override async void OnInitialize()
        {
            base.OnInitialize();
            Food = await NutritionSource.GetByIdAsync(id);

            MealList = Meal.DefaultList;
            SelectedMeal = MealList[0];
            DateList = DateNamed.DefaultList;
            SelectedDate = DateList[0];

            ServingIntList = Number.DefaultList;
            SelectedServingInt = ServingIntList[1];
            ServingDecList = Number.DefaultList;
            SelectedServingDec = ServingDecList[0];
        }
        #endregion

        #region Id
        private int id;
        private string paramId;
        public string ParamId
        {
            set
            {
                paramId = value;
                id = int.Parse(value);
            }
            get
            {
                return paramId;
            }
        }
        #endregion

        #region Food
        private Food food;
        public Food Food
        {
            get
            {
                return this.food;
            }

            set
            {
                this.food = value;
                NotifyOfPropertyChange("Food");
            }
        }
        #endregion

        #region Amount
        private List<Number> servingIntList;
        public List<Number> ServingIntList
        {
            get
            {
                return this.servingIntList;
            }

            set
            {
                this.servingIntList = value;
                NotifyOfPropertyChange("ServingIntList");
            }
        }

        private Number selectedServingInt;
        public Number SelectedServingInt
        {
            get
            {
                return this.selectedServingInt;
            }

            set
            {
                this.selectedServingInt = value;
                NotifyOfPropertyChange("SelectedServingInt");
            }
        }

        private List<Number> servingDecList;
        public List<Number> ServingDecList
        {
            get
            {
                return this.servingDecList;
            }

            set
            {
                this.servingDecList = value;
                NotifyOfPropertyChange("ServingDecList");
            }
        }

        private Number selectedServingDec;
        public Number SelectedServingDec
        {
            get
            {
                return this.selectedServingDec;
            }

            set
            {
                this.selectedServingDec = value;
                NotifyOfPropertyChange("SelectedServingDec");
            }
        }
        #endregion
    }
}
